﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SPCFMedicineInformer.DocumentTypes;
using Vega.USiteBuilder;
using SPCFMedicineInformer.Members;
using System.Web.Security;

using umbraco.MacroEngines;
using umbraco.NodeFactory;
using umbraco;
using Fonqi.Umbraco;
using umbraco.cms.businesslogic.web;



namespace SPCFMedicineInformer.Masterpages
{
    public partial class Admin_MedicineListings : TemplateBase<AdminContentPage>
    {

        public int NodeToDeleteId { get; set; }
        public int PageNum { get; set; }
        public int TotalRec { get; set; }
        int limitPerPage = 20;
        protected void Page_Load(object sender, EventArgs e)
        {      

            if (!IsPostBack)
            {
                int tempInt;
                PageNum = 1;
                BindGrid();
                BindDropdown();
            }
            
        }
        void BindDropdown()
        {
            var catList = NodeLocator.GetNodesOfExactType<MedicineCategory>();
            ddlCategories.DataSource = catList;
            ddlCategories.DataTextField = "Name";
            ddlCategories.DataValueField = "Id";
            ddlCategories.DataBind();
            ddlCategories.Items.Insert(0, new ListItem("Choose...", ""));


            ddlTitles.Items.Add(new ListItem("Id","Id"));
            ddlTitles.Items.Add(new ListItem("Category", "Category"));
            ddlTitles.Items.Add(new ListItem("Generic Name", "Generic Name"));
            ddlTitles.Items.Add(new ListItem("Medicine Name", "Medicine Name"));
            //ddlTitles.Items.Add(new ListItem("Indications", "Indications"));
            //ddlTitles.Items.Add(new ListItem("Dosages", "Dosages"));
            //ddlTitles.Items.Add(new ListItem("Contradictions", "Contradictions"));
            //ddlTitles.Items.Add(new ListItem("Special Precautions", "Special Precautions"));
            //ddlTitles.Items.Add(new ListItem("Presentation/Packaging", "Presentation/Packaging"));
            //ddlTitles.Items.Add(new ListItem("Adverse Reactions", "Adverse Reactions"));
            //ddlTitles.Items.Add(new ListItem("Pregnancy Safety", "Pregnancy Safety"));

        }
        void BindGrid()
        {
            IEnumerable<MedicineItem> medList = NodeLocator.GetNodesOfExactType<MedicineItem>().Where(a => 
                                a.MedicineName.ToLower().ContainsAny(txtSearchMedicine.Text.ToLower()) &&
                                a.CategoryId.ToLower().ContainsAny(ddlCategories.SelectedValue));
            medList = Sort(medList);
            TotalRec = medList.Count();
            medList = medList.Skip((PageNum - 1) * limitPerPage).Take(limitPerPage);
            DisplayPaging();
            RepMedicineItem.DataSource = medList;
            RepMedicineItem.DataBind();         
        }
        void DisplayPaging()
        {
            int maxPage = (TotalRec / limitPerPage) + (TotalRec % limitPerPage > 0 ? 1 : 0);
            List<string> pageList = new List<string>();
            for (int i = 1; i <= maxPage; i++)
            {
                pageList.Add(i.ToString());
            }
            
            rptPaging.DataSource = pageList;
            rptPaging.DataBind();

        }
        protected void btnAddMedicine_Click(object sender, EventArgs e)
        {
            Response.Redirect("/admin/utilities/medicine-entry.aspx");

        }
        void Delete(int id)
        {
            if (id > 0)
            {
                Document doc = new Document(id);
                if (doc.Published)
                {
                    doc.UnPublish();
                    library.UnPublishSingleNode(doc.Id);
                }
                doc.delete();
                BindGrid();
            }
        }
        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case "delete":
                        Delete(intTemp);
                        break;
                    case "paging":
                        PageNum = intTemp;
                        BindGrid();
                        break;
                }
            }
        }
        IEnumerable<MedicineItem> Sort(IEnumerable<MedicineItem> medList)
        {
            if (rdoSort.Text == "Asc")
            {
                switch (ddlTitles.SelectedValue)
                {
                    case "Id":
                        medList = medList.OrderBy(a => a.Id);
                        break;
                    case "Category":
                        medList = medList.OrderBy(a => a.CategoryName);
                        break;
                    case "Generic Name":
                        medList = medList.OrderBy(a => a.GenericName);
                        break;
                    case "Medicine Name":
                        medList = medList.OrderBy(a => a.MedicineName);
                        break;
                }
            }
            else
            {
                switch (ddlTitles.SelectedValue)
                {
                    case "Id":
                        medList = medList.OrderByDescending(a => a.Id);
                        break;
                    case "Category":
                        medList = medList.OrderByDescending(a => a.CategoryName);
                        break;
                    case "Generic Name":
                        medList = medList.OrderByDescending(a => a.GenericName);
                        break;
                    case "Medicine Name":
                        medList = medList.OrderByDescending(a => a.MedicineName);
                        break;
                }
            }
            return medList;
        }
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            PageNum = 1;
            BindGrid();
        }

        protected void Repeater_ItemDatabound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                LinkButton lb = (LinkButton)e.Item.FindControl("linkButPaging");
                Literal lit = (Literal)e.Item.FindControl("litPaging");
                if (PageNum.ToString() == lit.Text)
                {
                    lb.CssClass = "current";
                }
            }
            
        }
        
    }
}